import{p as A,l as q,U as F,aS as $,O,N as W,s as j,a2 as D}from"./DrFvzlh8agutd.js";import{ac as G}from"./MpnPZlTSagutd.js";import{e as P,f as I}from"./DZFz6IIX1esOcA.js";import{a as V}from"./BPirqa3s3yM15e.js";import{f as S}from"./sf48gIWk3aD96J.js";import{p as v}from"./Ba8fE4Bu4swc5U.js";const _=A("pdf-thumbnail"),z=800;async function E(h){try{const s={},o=await h.getPage(1),u=o.getViewport({scale:1}),p=z/u.width,b=o.getViewport({scale:p}),d=document.createElement("canvas"),R=d.getContext("2d",{alpha:!1});return s.scale=p,s.height=b.height,s.width=b.width,d.height=b.height,d.width=b.width,await o.render({canvasContext:R,viewport:b}).promise,await new Promise(g=>d.toBlob(g,"image/png"))}catch(i){_.warn("generateThumbnail error",i)}}const k=location.protocol.startsWith("http"),H=`${k?`${location.protocol}//${location.host}`:"http://localhost"}/_webcache`,U=h=>k?h:new Request(`${H}/${h}`);async function N(h="r2cache"){const i=await caches.open(h);return{cache:i,async keys(){return(await i.keys()).map(s=>new URL(s.url).pathname.substring(1))},async entries(){return await i.keys()},async loadBlob(s){const o=await i.match(U(s));if(o)return await o?.blob()},async getAsUrl(s){const o=await i.match(U(s));if(o){const u=await o?.blob();if(u)return URL.createObjectURL(u)}},async putBlob(s,o){const u=new Response(o);await i.put(U(s),u)},async put(s,o,u="image/png"){const p=new Blob([o],{type:u}),b=new Response(p);await i.put(U(s),b)}}}const e=A("app:pdf-cache");function J(){e("usePdfCache");const h={},i=q({});let s;async function o(){return s===void 0&&(s=await N()),s}const u=S({name:"thumbnailsCache",maxParallel:20,idConflictResolution:"prioritize"});async function p(t,r,n=!0){e("cachedUrl thumbnail",t,u);const c=h[t];if(c)return e("cachedUrl found in memcache",t,c),c;const l=await o();if(l){const a=await l.getAsUrl(t);if(a)return e("cachedUrl found in webcache",t,a),a}if(n&&r){const a=async()=>{try{e("cachedUrl thumbnail generate task",t);const f=await r();if(f){const y=f instanceof Blob?f:new Blob([G(f)]),w=await o();e("cachedUrl webcache",w!=null),w&&await w.putBlob(t,y);const C=URL.createObjectURL(y);return h[t]=C,C}else e.warn("cachedUrl did not get data",t)}catch(f){e.warn("cachedUrl err",f)}},{promise:m}=u.enqueue(a,{id:t});return await m}}async function b(t,r,n=!1){e("thumbnail: thumbnail",t,u);const c=h[t];if(c)return e("thumbnail: found in memcache",t,c),c;const l=await o();if(l){const a=await l.getAsUrl(t);if(a)return e("thumbnail: found in webcache",t,a),a}if(n){const{promise:a}=u.enqueue(async()=>{e("thumbnail: thumbnail generate task",t);const m=await r();if(m)return e("thumbnail: thumbnail with data",m),await g(t,m);e.warn("thumbnail did not get data",t)});return await a}}async function d(t,r=!0){const n=P(t);if(n)return await p(n,async()=>{const l=await(j.workspace?.sync?.extLocal).thumbnail(t.cid,t.idx),a=await D(l);if(e("thumbnailForAsset: received data",a?.length),a)return a},r)}async function R(t,r,n=!0){return await b(t,async()=>r,n)}async function B(t,r){const n=await v(r);if(e("loaded pdf source",n,t,r),n)return g(t,n)}async function g(t,r){try{const n=await E(r);if(r.destroy(),n!=null){const c=await o();c&&await c.putBlob(t,n);const l=URL.createObjectURL(n);return h[t]=l,l}}catch(n){e.warn("addDocument error",t,n)}}function x(t){u.cancel(t)}function T(t){const r=F();function n(c){if(!c||i[c]!=null)return;const l=I(c);if(l==null)return;const a=P(l);u.enqueue(async m=>{const f=h[a];if(f){e("thumbnail: found in memcache",a,f),i[c]=f;return}const y=await o();if(y){const w=await y.getAsUrl(a);if(w){e("thumbnail: found in webcache",a,w),i[c]=w;return}}m.progress.isCancelled()||(e("thumbnail:generate"),r.add(V(c,async w=>{if(e("thumbnail: onAsset",c),w?.bin){const C=await v(w?.bin);if(C){const L=await g(a,C);L&&(i[c]=L)}}})))},{id:a})}$(t)&&(e("watch",t),O(t,n,{immediate:!0}),W(r))}return{thumbnails:i,thumbnailForAsset:d,thumbnailForPdf:R,addDocumentBySource:B,cancel:x,useThumbnail:T}}const tt=J();export{tt as p};